package com.knowif.backend.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.knowif.backend.dto.OfferDto;
import com.knowif.backend.service.OfferService;
import com.knowif.backend.service.SchoolService;
import com.knowif.backend.service.StanderSorceService;
import com.knowif.pojo.*;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;
import com.knowif.util.ExampleUtil;
import com.knowif.util.commonUtil.ObjectUtil;
import com.knowif.util.commonUtil.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ：Xh
 * @date ：Created in 2019/6/10 0010 20:50
 */
@RestController
@RequestMapping("/wechat")
public class OfferController {

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private OfferService offerService;

    @Autowired
    private StanderSorceService standerSorceService;

    @PostMapping("/offer/query")
    public RestBean<?> query(@RequestBody SearchCondition searchCondition) {
        try {
            SchoolExample schoolExample = ExampleUtil.buildExample(new SchoolExample(), searchCondition);
            List<School> schools = schoolService.selectSchool(schoolExample, false);
            return RestBean.buildSuccess(schools);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @PostMapping("/offer/saveOfferAndSorce")
    @Transactional
    public RestBean saveOfferAndSorce(@RequestBody OfferDto offerDto) {
        try {

            offerDto.getStanderSorce().setCreateTime(new Date());
            StanderSorce standerSorce = offerDto.getStanderSorce();
            String scores = standerSorce.getScores();
            //处理scores
            String newScores = "{\"TOEFL\":{\"R\":\"\",\"L\":\"\",\"S\":\"\",\"W\":\"\",\"SUM\":\"\"},\"IELTS\":{\"R\":\"\",\"L\":\"\",\"S\":\"\",\"W\":\"\",\"SUM\":\"\"},\"GRE\":{\"V\":\"\",\"Q\":\"\",\"AW\":\"\"},\"SUB\":{\"PRO\":\"\",\"S\":\"\"},\"GMAT\":{\"Q\":\"\",\"V\":\"\",\"SUM\":\"\"},\"LSAT\":{\"S\":\"\"},\"GKCJ\":{\"JG\":\"\",\"SF\":\"\",\"Y\":\"\",\"S\":\"\",\"W\":\"\",\"ZH\":\"\",\"SUM\":\"\"},\"KYCJ\":{\"Y\":\"\",\"Z\":\"\",\"S\":\"\",\"P1\":\"\",\"P2\":\"\",\"SUM\":\"\"}}";
            if (!StringUtil.isEmpty(scores)) {
                newScores = scores.substring(1, scores.length() - 1);
            }
            standerSorce.setScores(newScores);
            //如果为空，填充空值
            if (StringUtils.isEmpty(standerSorce.getBkSchoolType())) {
                standerSorce.setBkSchoolType("");
            }
            if (StringUtils.isEmpty(standerSorce.getYjsSchoolType())) {
                standerSorce.setYjsSchoolType("");
            }
            if (StringUtils.isEmpty(standerSorce.getBkEducationYear())) {
                standerSorce.setBkEducationYear("");
            }
            if (StringUtils.isEmpty(standerSorce.getYjsEducationYear())) {
                standerSorce.setYjsEducationYear("");
            }
            if (StringUtils.isEmpty(standerSorce.getBkRank())) {
                standerSorce.setBkRank("");
            }
            if (StringUtils.isEmpty(standerSorce.getYjsRank())) {
                standerSorce.setYjsRank("");
            }
            if (StringUtils.isEmpty(standerSorce.getBkSpecial())) {
                standerSorce.setBkSpecial("");
            }
            if (StringUtils.isEmpty(standerSorce.getYjsSpecail())) {
                standerSorce.setYjsSpecail("");
            }
            if (StringUtils.isEmpty(standerSorce.getOtherInstructions())) {
                standerSorce.setOtherInstructions("");
            }
            if (StringUtils.isEmpty(standerSorce.getLeaveMessage())) {
                standerSorce.setLeaveMessage("");
            }
            Integer standerScoreId = standerSorceService.saveStanderScore(standerSorce);
            offerDto.getOffer().setCreateTime(new Date());
            offerDto.getOffer().setStanderScoreId(standerScoreId);
            offerService.saveOffer(offerDto.getOffer());
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RestBean.buildError("参数错误");
        }
        return RestBean.build(RestBean.success_code);
    }

    @PostMapping("/offer/save")
    @Transactional
    public RestBean save(@RequestBody Offer offer) {
        offer.setCreateTime(new Date());
        offerService.saveOffer(offer);
        return RestBean.build(RestBean.success_code);
    }

    @PostMapping("/offer/update")
    @Transactional
    public RestBean update(@RequestBody Offer offer) {
        offerService.updateOffer(offer);
        return RestBean.buildSuccess("修改成功!");
    }


    @GetMapping("/offer/detail")
    public RestBean<?> offerDetail(@RequestParam("userId") int userId, @RequestParam("offerId") int offerId) {
        return offerService.detail(offerId, userId);
    }

}
